import sys

from PyQt5 import uic, QtCore
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QMainWindow, QSystemTrayIcon, QAction, QMenu

from qtchao.QtUtils import showMessageBox
from utils import CommonHelper
from utils.MouseMoveFilter import MouseMoveFilter


class Window(QMainWindow):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        uiFileName = CommonHelper.getFilePath("./uifiles/hello.ui")
        uic.loadUi(uiFileName, self)
        self.logo_img = CommonHelper.getFilePath("./uifiles/logo.ico")
        self.setWindowIcon(QIcon(self.logo_img))

        self.pushButton.clicked.connect(self.setQssStyle)
        self.eventFilter = MouseMoveFilter(self)

        # self.setQssStyle()
        self.setWindowStyle()
        self.bindMoveEvent()

    def showEvent(self, e):
        QMainWindow.showEvent(self, e)
        # 显示托盘，必须在窗口显示完成之后
        self.addSystemTray()

    def setQssStyle(self):
        qssFileName = CommonHelper.getFilePath('./uifiles/Aqua.qss')
        qssStyle = CommonHelper.readContent(qssFileName)
        self.setStyleSheet(qssStyle)

    def setWindowStyle(self):
        # 无边框
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        self.setWindowOpacity(0.95)  # 设置窗口透明度
        # self.setAttribute(Qt.WA_TranslucentBackground)  # 设置窗口背景透明

    def bindMoveEvent(self):
        self.eventFilter.watch = self.menubar
        self.menubar.installEventFilter(self.eventFilter)

    # 添加中文的退出提示框2
    def closeEvent(self, event):
        message = showMessageBox(self, "确认窗口", "确定退出吗？")
        # 判断返回值，如果点击的是Yes按钮，我们就关闭组件和应用，否则就忽略关闭事件
        if message:
            event.accept()
        else:
            event.ignore()

    def addSystemTray(self):
        minimizeAction = QAction("Mi&nimize", self, triggered=self.hide)
        maximizeAction = QAction("Ma&ximize", self, triggered=self.showMaximized)
        restoreAction = QAction("&Restore", self, triggered=self.showNormal)
        quitAction = QAction("&Quit", self, triggered=self.close)
        self.trayIconMenu = QMenu(self)
        self.trayIconMenu.addAction(minimizeAction)
        self.trayIconMenu.addAction(maximizeAction)
        self.trayIconMenu.addAction(restoreAction)
        self.trayIconMenu.addSeparator()
        self.trayIconMenu.addAction(quitAction)
        self.trayIcon = QSystemTrayIcon(self)
        self.trayIcon.setIcon(QIcon(self.logo_img))
        # 鼠标移动到托盘上时显示的文字
        self.trayIcon.setToolTip("PyQt5")
        self.trayIcon.setContextMenu(self.trayIconMenu)
        self.trayIcon.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = Window()
    win.show()
    app.exec_()
